<?php

App::uses('AppController', 'Controller');

class AdminController extends AppController {
    
    public $components = array(
        'Auth' => array(
            'loginAction' => '/admin/login'
        )
    );
    
    public $layout = 'admin';
    public $uses = array('Settings');
    
    public function login() {
        if ($this->Auth->user('id')) {
            $this->redirect('index');
        }
        
        $this->layout = 'login';
        
        $data = $this->data;
        if (! empty($data)) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->setFlash('Prijava nije uspela.');
            }
        }
    }
    
    public function logout() {
        $this->redirect($this->Auth->logout());
    }
    
    public function index() {
        $this->set('data', $this->Settings->find('all'));
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            
            if ($this->Settings->saveAll($data)) {
                $this->setFlash('Podešavanja su uspešno sačuvana.', 'ok');
            } else {
                $this->setFlash('Greška pri čuvanju podataka.');
            }
        }
        $this->redirect('index');
    }
    
}
